package com.bykj.property.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bykj.property.controller.vo.request.PendingPaymentRecordPageRequest;
import com.bykj.property.controller.vo.view.PendingPaymentRecordVo;
import com.bykj.property.mapper.PaymentRecordMapper;
import com.bykj.property.mapper.PmProprietorMapper;
import com.bykj.property.model.PendingPaymentRecord;
import com.bykj.property.mapper.PendingPaymentRecordMapper;
import com.bykj.property.model.PmProprietor;
import com.bykj.property.model.SysRole;
import com.bykj.property.service.PendingPaymentRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
* <p>
    * 待缴费记录表 服务实现类
    * </p>
*
* @author liangyiming
* @since 2019-06-20
*/
@Service
@Slf4j
public class PendingPaymentRecordServiceImpl extends ServiceImpl<PendingPaymentRecordMapper, PendingPaymentRecord> implements PendingPaymentRecordService {
@Autowired
private PendingPaymentRecordMapper mapper;
    @Autowired
private PmProprietorMapper pmProprietorMapper;
    @Autowired
    private PaymentRecordMapper paymentRecordMapper;

    @Override
    public Boolean createPendingPaymentRecord(Long pmVillageId, Long chargeTypeId, BigDecimal price, Date start, Date end) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("pm_village_id",pmVillageId);
        List<PmProprietor> list=pmProprietorMapper.selectList(queryWrapper);

        for(PmProprietor pm:list){

            PendingPaymentRecord record=new PendingPaymentRecord();
            record.setChargeTypeId(chargeTypeId);
            record.setPmProprietorId(pm.getPmProprietorId());
            record.setPmUnitId(pm.getPmUnitId());
            record.setPmVillageId(pm.getPmVillageId());
            record.setPropName(pm.getPropName());
            record.setDoorNumber(pm.getDoorNumber());
            record.setPendingChargePrice(price.multiply(new BigDecimal(pm.getHouseArea().toString())).toString());
            QueryWrapper payWrapper=new QueryWrapper();
            payWrapper.eq("pm_proprietor_id",pm.getPmProprietorId());
            payWrapper.ge("start_time",start);
            payWrapper.le("start_time",start);
            payWrapper.ge("end_time",start);
            payWrapper.le("end_time",start);
            List paylist=paymentRecordMapper.selectList(payWrapper);
            record.setPendingStartTime(start);
            record.setPendingEndTime(end);

            if(paylist==null||paylist.size()<0) {
                mapper.insert(record);
            }
        }
        return true;
    }

    @Override
public Page<PendingPaymentRecord> queryPage(PendingPaymentRecordPageRequest pageRequest) {

    return mapper.queryPage(pageRequest);

    }


    }